package romove.duplicate.data;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * Created by luoqifei on 2016/11/2.
 */
//in the file removeDup1.txt removeDup2.txt,there are same data is duplicate
public class RemoveDup {
    public static class Map extends Mapper<Object,Text,Text,Text >{
        private static Text line = new Text();
        public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
        line = value;
            //just output the value,as reduce key
        context.write(line,new Text(""));
        }
    }
    public static class Reduce extends Reducer<Text,Text,Text,Text>{
        public void reduce (Text key,Iterable<Text> values,Context context ) throws IOException,InterruptedException{
            context.write(key,new Text(""));
        }
    }
    public static void main(String args[]) throws IOException {
        Configuration conf = new Configuration();
        //we need to put removeDup1.txt removeDup2.txt in hdfs /user/dup_in/
        String inputSrc = "hdfs://localhost:9000/user/dup_in";
        //this output src must be not exists
        String outputSrc = "hdfs://localhost:9000/user/dup_out";
        conf.set("mapred.job.tracker","localhost:9000");
        Job job = new Job(conf,"data duplication deal");
        //set jar by class for java -jar
        job.setJarByClass(RemoveDup.class);
        //set map and reduce
        job.setMapperClass(Map.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);
        //set out put key and value type
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job,new Path(inputSrc));
        FileOutputFormat.setOutputPath(job,new Path(outputSrc));
        try {
            job.waitForCompletion(true);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
